#include<stdbool.h>
typedef int Item;
#define MAXQUEUE 100
typedef struct node{
	Item item;
	struct node *next;
}Node;
typedef struct queue{
Node *front;
Node *rear;
int items;	
}Queue;
void InitQueue(Queue *pq);
bool QueueIsFull(const Queue *pq);
bool QueueIsEmpty(const Queue *pq);
int QueueItemCount(const Queue *pq);
bool EnQueue(Item item,Queue *pq);
bool DeQueue(Item *pitem,Queue *pq);
void EmptyQueue(Queue *pq);

static void CopyToNode(Item item,Node *pn);
static void CopyToItem(Node *pn, Item *pi);

void InitQueue(Queue *pq){
pq->front = pq->rear = NULL;
pq->items = 0;	
}

bool QueueIsFull(const Queue *pq){
	return pq->items == MAXQUEUE;	
}
bool QueueIsEmpty(const Queue *pq){
	return pq->items==0;	
}
int QueueItemCount(const Queue *pq){
	return pq->items;	
}
bool EnQueue(Item item, Queue *pq){
	Node *pnew;
	if(QueueIsFull(pq))return false;
	pnew = (Node*)malloc(sizeof(Node));
	if(pnew == NULL){
		fprintf(stderr,"Unable to allocate memory\n");
		return false;	
	}
	CopyToNode(item,pnew);
	pnew->next = NULL;
	if(QueueIsEmpty(pq)) pq->front = pnew;
	else pq->rear->next = pnew;
	pq->rear = pnew;
	pq->items++;
	return true;
}

bool DeQueue(Item *pitem,Queue *pq){
	Node *pt;
	if(QueueIsEmpty(pq))return false;
	CopyToItem(pq->front,pitem);
	pt = pq->front;
	pq->front = pq->front->next;
	free(pt);
	pq->items--;
	if(pq->items==0)pq->rear = NULL;
	
	return true;
}

void EmptyQueue(Queue *pq){
	Item dummy;
	while(!QueueIsEmpty(pq))
		DeQueue(&dummy,pq);	
}
static void CopyToNode(Item item,Node *pn){
	pn->item = item;	
}
static void CopyToItem(Node *pn,Item *pi){
	*pi = pn->item;	
}